#include<iostream>
using namespace std;
const int N = 15;
int mp[N+5][N+5];//地图
int vis[N+5][N+5];//判断是否被访问过
int dir[4][2] = { {-1,0},{1,0},{0,-1},{0,1} };//移动方向
int sum, ans = 100000;
int n, m;//n行，m列
void dfs(int x, int y, int c, int s)
{
    if (2 * s > sum)return;
    if (2 * s == sum )//符合题目要求并且包含左上角那一部分
    {
        if (c < ans && vis[0][0])
        {
            ans = c;
        }
        return;
    }
    vis[x][y] = 1;
    for (int i = 0; i < 4; i++)
    {
        int nx = x + dir[i][0];
        int ny = y + dir[i][1];
        if (nx >= 0 && nx < n && ny >= 0 && ny < m && !vis[nx][ny])
            dfs(nx, ny, c + 1, s+mp[x][y]);
    }
    vis[x][y] = 0;
}
int main()
{
    scanf("%d%d", &m, &n);
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            scanf("%d", &mp[i][j]),sum+=mp[i][j];
    dfs(0, 0, 0, 0);
    cout << (ans==100000?0:ans);
    return 0;
}